﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using SdmxMl.Structure;
using SdmxMl.Common;
using SdmxMl.Manager;

namespace SdmxMl.Msg
{
    public class HeaderGenerator
    {
        private static bool _nsiInfoFetched;
        private static OrganisationSchemesType _nsiOrgScheme;
        public static OrganisationSchemesType NSI_OrgScheme
        {
            get
            {
                if (_nsiInfoFetched == false)

                    try
                    {
                        _nsiInfoFetched = true;
                        if (ConfigurationManager.AppSettings["NSI_OrgScheme_Urn"] != null)
                        {
                            string url = ConfigurationManager.AppSettings["NSI_OrgScheme_Urn"].ToString().Trim();
                            SmManager sm = new SmManager();
                            ArtefactRefTyped art = UrnManager.UrnToArtefactRefTyped(url);
                            ArtefactRefTypedList list = new ArtefactRefTypedList();
                            list.Add(art);

                            sm.LoadMissingArtefactsFromRegistry(list);
                            _nsiOrgScheme = sm.GetArtefact(art) as OrganisationSchemesType;
                        }
                    }
                    catch
                    {
                    }

                return _nsiOrgScheme;
            }
        }
        private HeaderGenerator()
        {
        }

        public static HeaderType GetHeader()
        {
            HeaderType header = new HeaderType();

            header.MsgName.UpdateForLanguage("en", ConfigurationManager.AppSettings["StructureHeaderMsgName"]);
            header.Test = ConfigurationManager.AppSettings["StructureHeaderIsTest"].ToLower() == "true";
            ContactType ct = new ContactType();
            ct.Name.UpdateForLanguage("en", ConfigurationManager.AppSettings["StructureHeaderContactName"]);
            ct.Department.UpdateForLanguage("en", ConfigurationManager.AppSettings["StructureHeaderContactDept"]);
            ct.ContactInfo.TelephoneList.Add(ConfigurationManager.AppSettings["StructureHeaderContactInfoTel"]);
            ct.ContactInfo.FaxList.Add(ConfigurationManager.AppSettings["StructureHeaderContactInfoFax"]);
            header.Sender.Contact.Add(ct);
            header.Sender.Name.UpdateForLanguage("en", ConfigurationManager.AppSettings["StructureHeaderSenderName"]);
            header.Sender.Id = ConfigurationManager.AppSettings["StructureHeaderSenderId"];
            return header;
        }

        public static HeaderType GetHeader(string fullId)
        {
            HeaderType header = new HeaderType();
            if (NSI_OrgScheme == null || string.IsNullOrEmpty(fullId))
                header = GetHeader();
            else
            {
                try
                {
                    // Uses NSI Organisation scheme info as Contact (if found)
                    string[] arr = fullId.Split('+');

                    ISdmxHierarchyListItem item = NSI_OrgScheme.Agencies.LocateItem(arr[0]);
                    OrganisationType org = item as OrganisationType;
                    if (org != null && org.MaintenanceContact != null && org.MaintenanceContact.IsEmpty == false)
                    {
                        header.MsgName.UpdateForLanguage("en", ConfigurationManager.AppSettings["StructureHeaderMsgName"]);
                        header.Test = ConfigurationManager.AppSettings["StructureHeaderIsTest"].ToLower() == "true";

                        // Transmit Organisation group contact
                        org.MaintenanceContact.Id = string.Empty;
                        header.Sender.Contact.Add(org.MaintenanceContact);

                        header.Sender.Name.UpdateForLanguage("en", ConfigurationManager.AppSettings["StructureHeaderSenderName"]);
                        header.Sender.Id = ConfigurationManager.AppSettings["StructureHeaderSenderId"];

                    }
                    else
                        header = GetHeader();
                }
                catch
                {
                    header = GetHeader();
                }
            }


            return header;
        }

    }
}
